package com.leo.train.business.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leo.train.business.dto.TrainDto;
import com.leo.train.business.dto.TrainQueryDto;
import com.leo.train.business.entity.Train;
import com.leo.train.business.mapper.TrainMapper;
import com.leo.train.business.service.TrainService;
import com.leo.train.business.vo.TrainQueryVo;
import com.leo.train.common.enums.TrainTypeEnum;
import com.leo.train.common.exception.BusinessException;
import com.leo.train.common.exception.BusinessExceptionEnum;
import com.leo.train.common.resp.PageResult;
import com.leo.train.common.util.SnowUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TrainServiceImpl implements TrainService {
    @Autowired
    private TrainMapper trainMapper;
    /**
     * 保存车次
     * @param trainDto
     */
    @Override
    public void save(TrainDto trainDto) {
        Long id = trainDto.getId();
        if(id == null) {
            // 判断该车次是否已经存在
            Train train =  selectByCode(trainDto.getCode());
            if(train != null) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
            }
            // 新增
            trainDto.setId(SnowUtil.getSnowflakeNextId());
            trainDto.setCreateTime(new Date());
            trainDto.setUpdateTime(new Date());
            trainMapper.insert(trainDto);
        } else {
            // 编辑
            trainDto.setUpdateTime(new Date());
            trainMapper.update(trainDto);
        }

    }

    /**
     * 查询车次列表
     * @param trainQueryDto
     * @return
     */
    @Override
    public PageResult<TrainQueryVo> list(TrainQueryDto trainQueryDto) {
        PageHelper.startPage(trainQueryDto.getPage(), trainQueryDto.getPageSize());
        Page<Train> page = trainMapper.pageQuery(trainQueryDto);
        // 遍历列表，为列表多返回一个类型名称
        List<TrainQueryVo> trainQueryVoList = page.getResult().stream().map(train -> {
            TrainQueryVo trainQueryVo = new TrainQueryVo();
            BeanUtils.copyProperties(train, trainQueryVo);
            trainQueryVo.setTypeName(TrainTypeEnum.fromCode(train.getType()).getDescription());
            return trainQueryVo;
        }).collect(Collectors.toList());

        return new PageResult(page.getTotal(), trainQueryVoList);
    }

    @Override
    public TrainQueryVo detail(Long id) {
        Train train = trainMapper.selectById(id);
        if(train == null) {
            throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_NOT_EXIST);
        }
        TrainQueryVo trainQueryVo = new TrainQueryVo();
        BeanUtils.copyProperties(train, trainQueryVo);
        trainQueryVo.setTypeName(TrainTypeEnum.fromCode(train.getType()).getDescription());
        return trainQueryVo;
    }

    @Override
    public void delete(Long id) {
        trainMapper.deleteById(id);
    }

    @Transactional
    @Override
    public List<Train> listAll() {
        return trainMapper.selectAll();
    }

    /**
     * 定义一个唯一键方法
     */
    public Train selectByCode(String code) {
        Train train = trainMapper.selectByCode(code);
        if (train != null) {
            return train;
        }
        return null;
    }
}
